﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CSScripting;
using RAP.Framework.Libary.Utils;

namespace RAP.Framework.Libary.DataSource.Model.DataSource
{
  /// <summary>
  /// 数据源配置集合
  /// </summary>
  public class DataSourceOptionCollection : Dictionary<string, IList<DataSourceOption>>
  {
    /// <summary>
    /// 
    /// </summary>
    public DataSourceOptionCollection() { }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="d"></param>
    public DataSourceOptionCollection(IDictionary<string, IList<DataSourceOption>> d) : base(d) { }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="d"></param>
    public DataSourceOptionCollection(IDictionary d)
    {
      d.CopyTo(this, copyValue: (src, dest, key, val) =>
      {
        var config = val as IDictionary;
        if (config == null)
        {
          return;
        }
        var list = new List<DataSourceOption>();
        var connection = config.ToString("ConnectionString");
        var name = config.ToString("Name");
        var dataSourceOption = config.CopyTo(new Hashtable());
        if (name.IsNotNullOrEmpty())
        {
          dataSourceOption["Name"] = $"{name}_{list.Count}";
        }
        list.Add(new DataSourceOption(dataSourceOption));
        dest[key] = list;
      });
    }

    public DataSourceOptionCollection Clone()
    {
      return new DataSourceOptionCollection(this as IDictionary<string, IList<DataSourceOption>>);
    }
  }
}
